package com.clk.portfoy.engines;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.clk.portfoy.PortfoyStatics;
import com.clk.portfoy.classlib.piyasa.Piyasa;
import com.clk.portfoy.classlib.portfoy.DHisseSenedi;

public class WebEngine {
	// ---------------------------------------------------------------------------------
	public static void fiilAnlikSenetValues(HashMap<String, ArrayList<DHisseSenedi>> senetler) throws IOException {
		String urlMilliyetFinans = " http://finans.milliyet.com.tr/portal/ISE/Canli-Borsa.html?firstCharacter=";
		String searchTag = "var SYMBOLS_";
		String lastPriceTag = "\"lastPrice\":";
		String changePercentageTag = "\"changePercentage\":";
		System.out.println("****************************" + PortfoyStatics.getDateStr(new Date()) + "************************");
		Iterator<?> it = senetler.entrySet().iterator();
		while (it.hasNext()) {
			@SuppressWarnings("rawtypes")
			Map.Entry pairs = (Map.Entry) it.next();

			String letter = (String) pairs.getKey();
			@SuppressWarnings("unchecked")
			ArrayList<DHisseSenedi> senetList = (ArrayList<DHisseSenedi>) pairs.getValue();
			// *********
			URL milliyetFinans = new URL(urlMilliyetFinans + letter);
			URLConnection yc = milliyetFinans.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
			String inputLine;
			String symbolsLine = "";
			while ((inputLine = in.readLine()) != null) {
				if (inputLine.contains(searchTag)) {
					symbolsLine = inputLine;
					break;
				}
			}
			// System.out.println(symbolsLine);
			for (int i = 0; i < senetList.size(); i++) {
				String senetAd = "\"" + senetList.get(i).getIsim() + "\"";
				int adIndex = symbolsLine.indexOf(senetAd, 0);
				if (adIndex >= 0) {
					/**
					 * lastPrice
					 */
					int lastPriceStartIndex = symbolsLine.indexOf(lastPriceTag, adIndex) + lastPriceTag.length();
					int lastPriceEndIndex = symbolsLine.indexOf(",", lastPriceStartIndex);
					String lastPriceStr = symbolsLine.substring(lastPriceStartIndex, lastPriceEndIndex);
					senetList.get(i).setAnlikDeger(Double.parseDouble(lastPriceStr));
					/**
					 * seanslikDegisim
					 */
					int changePercentageStartIndex = symbolsLine.indexOf(changePercentageTag, adIndex) + changePercentageTag.length();
					int changePercentageEndIndex = symbolsLine.indexOf(",", changePercentageStartIndex);
					String changePercentageStr = symbolsLine.substring(changePercentageStartIndex, changePercentageEndIndex);
					senetList.get(i).setChangePercentage(Double.parseDouble(changePercentageStr));
					System.out.println(senetList.get(i).getIsim() + ": " + lastPriceStr + "\t; KZ: "
							+ PortfoyStatics.getDoubleStr(senetList.get(i).getPotKZ())+ "\t; Seans Degisim: %" + changePercentageStr);
				} else {
					System.out.println(senetList.get(i).getIsim() + " Son Degerine Ulasilamadi");
				}
			}
			// sortOut(symbolsLine);
			in.close();
			// ********
		}
		System.out.println("***********************************************************************");
	}

	// ---------------------------------------------------------------------------------
	/**
	 * private static void sortOut(String line) { ArrayList indexStartList =
	 * findIndex(line, "{", 0); for (int i = 0; i < indexStartList.size(); i++)
	 * { System.out.println(indexStartList.get(i)); } ArrayList indexEndList =
	 * findIndex(line, "}", 0); for (int i = 0; i < indexEndList.size(); i++) {
	 * System.out.println(indexEndList.get(i)); } }
	 * 
	 * // ----------------------------------------------------------------------
	 * -----------
	 * 
	 * private static ArrayList findIndex(String searchString, String subject,
	 * int fromIndex) { ArrayList indexList = null; int currentIndex =
	 * searchString.indexOf(subject, fromIndex); if (fromIndex == 0) { indexList
	 * = new ArrayList<>(); } else if (fromIndex > 0) { indexList =
	 * findIndex(searchString, subject, currentIndex + 1);
	 * indexList.add(currentIndex); } return indexList; }
	 **/
	// ---------------------------------------------------------------------------------
	public static void fiilPiyasaValues(Piyasa piyasa) throws IOException {
		String urlBigPara = "http://www.bigpara.com/index.html";
		System.out.println("****************************" + PortfoyStatics.getDateStr(new Date()) + "************************");
		URL bigPara = new URL(urlBigPara);
		URLConnection yc = bigPara.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
		String inputLine;
		String lines = "";
		while ((inputLine = in.readLine()) != null) {
			lines += inputLine;
		}
		in.close();
		Document doc = (Document) Jsoup.parse(lines);
		Elements hisseBar = ((Element) doc).getElementsByClass("kapanis");
		System.out.println("***************************PIYASA**********************************");
		// System.out.println(hisseBar );
		System.out.println("***********************************************************************");
		System.out.println("*****XU100\t:  " + hisseBar.get(0).childNode(0).toString());
		System.out.println("*****DOLAR\t:  " + hisseBar.get(1).childNode(0).toString());
		System.out.println("*****EURO\t:  " + hisseBar.get(2).childNode(0).toString());
		System.out.println("*****ALTIN\t:  " + hisseBar.get(3).childNode(0).toString());
		System.out.println("*****PETROL\t:  " + hisseBar.get(4).childNode(0).toString());
		System.out.println("*****BONO\t:  " + hisseBar.get(5).childNode(0).toString());
		System.out.println("***********************************************************************");

		piyasa.setAltinTL(Double.parseDouble(hisseBar.get(3).childNode(0).toString().replace(",", ".")));
		piyasa.setBono(Double.parseDouble(hisseBar.get(5).childNode(0).toString().replace(",", ".")));
		piyasa.setDolarTL(Double.parseDouble(hisseBar.get(1).childNode(0).toString().replace(",", ".")));
		piyasa.setEuroTL(Double.parseDouble(hisseBar.get(2).childNode(0).toString().replace(",", ".")));
		piyasa.setPetrol(Double.parseDouble(hisseBar.get(4).childNode(0).toString().replace(",", ".")));
		piyasa.setXu100(Double.parseDouble(hisseBar.get(0).childNode(0).toString()));
	}

	// ---------------------------------------------------------------------------------
	public static void fiilAtinValues() throws IOException {
		String urlBigPara = "http://www.bigpara.com/altin/";
		String senetIDTag = "h_td_fiyat_id_";
		System.out.println("****************************" + PortfoyStatics.getDateStr(new Date()) + "************************");
		URL bigPara = new URL(urlBigPara);
		URLConnection yc = bigPara.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
		String inputLine;
		ArrayList<String> lines = new ArrayList<String>();
		while ((inputLine = in.readLine()) != null) {
			if (inputLine.contains(senetIDTag)) {
				lines.add(inputLine);
			}
			in.close();
		}
		System.out.println("***********************************************************************");
	}
	// ---------------------------------------------------------------------------------
}
